package com.tongcao.cn.model;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @Author:tongcao
 * @Date:2019/4/1 14:13
 * Description:
 */
@Slf4j
public class PersonExt {


    public String getName() {
        return "tongcao";
    }


    public String getEmail() {
        return "503201142@qq.com";
    }


    public double getLength() {
        return 18.0;
    }

    public int getAge() {
        return 28;
    }

    public static String getAddress() {
        return "湖南湘潭";
    }


    public static String getCourse() {
        return "Java";
    }

    public Person getAllParams() {
        Person person = new Person();
        Field[] fields = person.getClass().getDeclaredFields();

        Class<? extends PersonExt> clazz = this.getClass();

        String methodName;
        Method method;

        for (Field field : fields) {
            if (!Modifier.isStatic(field.getModifiers())) {
                String fieldName = field.getName();
                String capitalize = StringUtils.capitalize(fieldName);

                methodName = "get" + capitalize;
                try {
                    method = clazz.getMethod(methodName, null);
                    Object value = method.invoke(this, null);
                    field.setAccessible(true);
                    field.set(person, value);
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }
        }

        return person;
    }


    public static void main(String[] args) {
        PersonExt personExt = new PersonExt();

        System.out.println(personExt.getAllParams());
        System.out.println(JSON.toJSONString(personExt));

        Field[] fields = personExt.getClass().getDeclaredFields();
        System.out.println(JSON.toJSONString(fields));
    }
}
